import React, { PureComponent } from 'react';
import { connect } from 'dva';
import { List, Card, Input, Button, Modal, Form } from 'antd';
import PageHeaderWrapper from '@/components/PageHeaderWrapper';
import Result from '@/components/Result';
import moment from 'moment';
import styles from './Diary.less';

const FormItem = Form.Item;

@connect(({ diary, loading }) => ({
  diary,
  loading: loading.models.list,
}))
@Form.create()
class Diary extends PureComponent {
  state = {
    visible: false,
    done: false,
    previewVisible: '',
  };

  formLayout = {
    labelCol: { span: 7 },
    wrapperCol: { span: 13 },
  };

  componentDidMount() {
    const { dispatch } = this.props;
    dispatch({
      type: 'diary/fetch',
    });
  }

  showModal = () => {
    this.setState({
      visible: true,
      current: undefined,
    });
  };

  showEditModal = item => {
    this.setState({
      visible: true,
      current: item,
    });
  };

  handleDone = () => {
    setTimeout(() => this.addBtn.blur(), 0);
    this.setState({
      done: false,
      visible: false,
    });
  };

  handleCancels = () => this.setState({ previewVisible: false });

  handleCancel = () => {
    setTimeout(() => this.addBtn.blur(), 0);
    this.setState({
      visible: false,
    });
  };

  render() {
    const {
      diary: { data },
      loading,
    } = this.props;
    const {
      form: { getFieldDecorator },
    } = this.props;
    const { visible, done, previewVisible, current = {} } = this.state;

    const modalFooter = done
      ? { footer: null, onCancel: this.handleDone }
      : { okText: '保存', onOk: this.handleSubmit, onCancel: this.handleCancel };

    const ListContent = ({ data: { balance, price, createdAt, remark } }) => (
      <div className={styles.listContent}>
        <div className={styles.listContentItem}>
          <span>余额</span>
          <p>{balance / 100}</p>
        </div>
        <div className={styles.listContentItem}>
          <span>金额</span>
          <p>{price / 100}</p>
        </div>
        <div className={styles.listContentItem}>
          <span>时间</span>
          <p>{moment(createdAt).format('YYYY-MM-DD')}</p>
        </div>
        <div className={styles.listContentItem}>
          <span>备注</span>
          <p>{remark}</p>
        </div>
      </div>
    );

    const getModalContent = () => {
      if (done) {
        return (
          <Result
            type="success"
            title="操作成功"
            description="已确认添加该图片"
            actions={
              <Button type="primary" onClick={this.handleDone}>
                知道了
              </Button>
            }
            className={styles.formResult}
          />
        );
      }
      // const imageUrl = this.state.imageUrl;
      return (
        <Form onSubmit={this.handleSubmit}>
          <FormItem visible={previewVisible} label="分类名称" {...this.formLayout}>
            {getFieldDecorator('name', {
              rules: [{ required: true, message: '请输入分类名称' }],
              initialValue: current.name,
            })(<Input placeholder="请输入" />)}
          </FormItem>
        </Form>
      );
    };
    return (
      <PageHeaderWrapper>
        <div className={styles.standardList}>
          <Card
            className={styles.listCard}
            bordered={false}
            title="日志管理"
            style={{ marginTop: 24 }}
            bodyStyle={{ padding: '0 32px 40px 32px' }}
          >
            <List
              size="large"
              rowKey="id"
              loading={loading}
              pagination={{
                ...data.pagination,
                onChange: page => {
                  const { dispatch } = this.props;
                  dispatch({
                    type: 'diary/fetch',
                    payload: page,
                  });
                },
              }}
              dataSource={data.list}
              item={data.list}
              renderItem={item => (
                <List.Item>
                  <List.Item.Meta title={`[${item.id}]`} description={item.intro} />
                  <ListContent data={item} />
                </List.Item>
              )}
            />
          </Card>
        </div>
        <Modal
          className={styles.standardListForm}
          width={640}
          bodyStyle={done ? { padding: '72px 0' } : { padding: '28px 0 0' }}
          destroyOnClose
          visible={visible}
          {...modalFooter}
        >
          {getModalContent()}
        </Modal>
      </PageHeaderWrapper>
    );
  }
}

export default Diary;
